package com.eol.admin.modules.business.content;

import com.eol.admin.modules.business.AdminCommonUtils;
import com.eol.admin.modules.logging.log.Log;
import com.eol.common.base.BaseResponseCode;
import com.eol.common.constants.BaseCommonConstants;
import com.eol.common.exception.BaseException;
import com.eol.common.sequence.SequenceGen;
import com.eol.entity.po.content.ZMedia;
import com.eol.mapper.config.tkMybatis.TableData;
import com.eol.service.content.ZMediaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: liulinhui
 * Date: 2020/6/4
 * Time: 1:08 PM
 * Description: ZMediaController
 */
@RestController
@RequestMapping("api/media")
public class ZMediaController {

    @Resource
    private ZMediaService zMediaService;
    @Resource
    private SequenceGen sequenceGen;
    @Resource
    private AdminCommonUtils adminCommonUtils;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResponseEntity<Object> list(@RequestParam(value = "page", defaultValue = BaseCommonConstants.PAGE_HELP_PAGE) Integer page,
                                       @RequestParam(value = "size", defaultValue = BaseCommonConstants.PAGE_HELP_LIMIT) Integer limit,
                                       @RequestParam(value = "name", required = false) String name,
                                       @RequestParam(value = "merchantId", required = false) String merchantId,
                                       @RequestParam(value = "type", required = false) Integer type,
                                       @RequestParam(value = "id", required = false) String id) {
        List<String> merchantList = adminCommonUtils.getSelectMerchant();
        PageHelper.startPage(page + 1, limit);
        List<ZMedia> list = zMediaService.selectByQuery(ZMedia.builder()
                .id(id)
                .type(type)
                .merchantIdList(merchantList)
                .merchantId(merchantId)
                .name(name)
                .build());
        PageInfo<ZMedia> pageInfo = new PageInfo<>(list);
        TableData<ZMedia> tableData = new TableData<>(pageInfo.getTotal(), list);
        PageHelper.clearPage();
        return new ResponseEntity<>(tableData, HttpStatus.OK);
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @Log("添加图片视频")
    @PreAuthorize("@el.check({'media:add'})")
    public ResponseEntity<Object> add(@RequestBody @Valid ZMedia zMedia) {
        String merchantId = adminCommonUtils.getUserMerchantId();
        zMedia.setId(sequenceGen.genMediaId());
        zMedia.setMerchantId(merchantId);
        zMedia.setCreateTime(new Date());
        zMedia.setUpdateTime(new Date());
        return new ResponseEntity<>(zMediaService.insertSelective(zMedia), HttpStatus.OK);
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @Log("修改图片视频")
    @PreAuthorize("@el.check({'media:update'})")
    public ResponseEntity<Object> update(@RequestBody @Valid ZMedia zMedia) {
        ZMedia old = zMediaService.selectById(zMedia.getId());
        adminCommonUtils.validateMerchant(old.getMerchantId());
        zMedia.setUpdateTime(new Date());
        return new ResponseEntity<>(zMediaService.updateSelectiveById(zMedia), HttpStatus.OK);
    }

    @RequestMapping(value = "del/{id}", method = RequestMethod.DELETE)
    @Log("删除图片视频")
    @PreAuthorize("@el.check({'media:del'})")
    public ResponseEntity<Object> add(@PathVariable("id") String id) {
        ZMedia zMedia = zMediaService.selectById(id);
        adminCommonUtils.validateMerchant(zMedia.getMerchantId());
        if (!zMedia.getCanDel()) throw new BaseException(BaseResponseCode.RESOURCE_CAN_NOT_DELETE);
        return new ResponseEntity<>(zMediaService.deleteById(id), HttpStatus.OK);
    }
}
